<?php
/**
 * SMemberController.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/26
 * Time: 22:34
 */
namespace app\common\auto_curd\php_system\controller;

use app\common\auto_curd\php_system\model\SMemberModel;
use think\Controller;

abstract class SMemberController extends Controller
{
    /**
     * 创建操作
     * @return \think\response\View|void
     */
    public function create()
    {
        if($this->request->isPost()){
            $params = $this->request->post();
            $addData = [];

            // ID
            if(isset($params['id']) && $params['id']!="")
            {
                $addData['id'] = $params['id'];
            }

            // 是否启用(0:否,1:是)
            if(isset($params['status']) && $params['status']!="")
            {
                $addData['status'] = $params['status'];
            }

            // 性别(0:无,1:男,2:女)
            if(isset($params['sex']) && $params['sex']!="")
            {
                $addData['sex'] = $params['sex'];
            }

            // 创建时间
            if(isset($params['create_ts']) && $params['create_ts']!="")
            {
                $addData['create_ts'] = $params['create_ts'];
            }

            // 修改时间
            if(isset($params['update_ts']) && $params['update_ts']!="")
            {
                $addData['update_ts'] = $params['update_ts'];
            }

            // 会员名
            if(isset($params['username']) && $params['username']!="")
            {
                $addData['username'] = $params['username'];
            }

            // 电话
            if(isset($params['phone']) && $params['phone']!="")
            {
                $addData['phone'] = $params['phone'];
            }

            // 密码提示
            if(isset($params['pawrd']) && $params['pawrd']!="")
            {
                $addData['pawrd'] = $params['pawrd'];
            }

            // 加密密码
            if(isset($params['password']) && $params['password']!="")
            {
                $addData['password'] = $params['password'];
            }

            // 昵称
            if(isset($params['nick_name']) && $params['nick_name']!="")
            {
                $addData['nick_name'] = $params['nick_name'];
            }

            // 真实姓名
            if(isset($params['real_name']) && $params['real_name']!="")
            {
                $addData['real_name'] = $params['real_name'];
            }

            // 邮箱
            if(isset($params['email']) && $params['email']!="")
            {
                $addData['email'] = $params['email'];
            }

            // 头像
            if(isset($params['avatar']) && $params['avatar']!="")
            {
                $addData['avatar'] = $params['avatar'];
            }

            // 会员简介
            if(isset($params['remark']) && $params['remark']!="")
            {
                $addData['remark'] = $params['remark'];
            }

            $status = SMemberModel::save(null,$addData);
            if(!$status)
            {
                return $this->error('添加失败');
            }
            else
            {
                return $this->success('添加成功',url('update')."?id=".$status);
            }
        }else{
            return view('common/auto_curd/php_system@s_member/create');
        }
    }
    
    /**
     * 更新操作
     */
    public function update()
    {
        if($this->request->isPost()){
            $params = $this->request->post();
            $saveData = [];

            // ID
            if(isset($params['id']) && $params['id']!="")
            {
                $saveData['id'] = $params['id'];
            }

            // 是否启用(0:否,1:是)
            if(isset($params['status']) && $params['status']!="")
            {
                $saveData['status'] = $params['status'];
            }

            // 性别(0:无,1:男,2:女)
            if(isset($params['sex']) && $params['sex']!="")
            {
                $saveData['sex'] = $params['sex'];
            }

            // 创建时间
            if(isset($params['create_ts']) && $params['create_ts']!="")
            {
                $saveData['create_ts'] = $params['create_ts'];
            }

            // 修改时间
            if(isset($params['update_ts']) && $params['update_ts']!="")
            {
                $saveData['update_ts'] = $params['update_ts'];
            }

            // 会员名
            if(isset($params['username']) && $params['username']!="")
            {
                $saveData['username'] = $params['username'];
            }

            // 电话
            if(isset($params['phone']) && $params['phone']!="")
            {
                $saveData['phone'] = $params['phone'];
            }

            // 密码提示
            if(isset($params['pawrd']) && $params['pawrd']!="")
            {
                $saveData['pawrd'] = $params['pawrd'];
            }

            // 加密密码
            if(isset($params['password']) && $params['password']!="")
            {
                $saveData['password'] = $params['password'];
            }

            // 昵称
            if(isset($params['nick_name']) && $params['nick_name']!="")
            {
                $saveData['nick_name'] = $params['nick_name'];
            }

            // 真实姓名
            if(isset($params['real_name']) && $params['real_name']!="")
            {
                $saveData['real_name'] = $params['real_name'];
            }

            // 邮箱
            if(isset($params['email']) && $params['email']!="")
            {
                $saveData['email'] = $params['email'];
            }

            // 头像
            if(isset($params['avatar']) && $params['avatar']!="")
            {
                $saveData['avatar'] = $params['avatar'];
            }

            // 会员简介
            if(isset($params['remark']) && $params['remark']!="")
            {
                $saveData['remark'] = $params['remark'];
            }

            $status = SMemberModel::save($saveData['id'],$saveData);
            if(!$status)
            {
                return $this->error('更新失败');
            }
            else
            {
                return $this->success('更新成功');
            }
        }else{
            $row = SMemberModel::get(['id'=>$this->request->get('id')]);
            return view('common/auto_curd/php_system@s_member/update',[
                'row' => $row
            ]);
        }
    }
    /**
     * 获取操作
     */
    public function read()
    {

        $list = SMemberModel::paginate([]);
        $pagination = $list->render();
        return view('common/auto_curd/php_system@s_member/read',[
            'list' => $list,
            'pagination' => $pagination
        ]);

    }
    /**
     * 删除操作
     */
    public function delete()
    {

        $status = SMemberModel::delete($this->request->param('id'));
        if(!$status)
        {
            return $this->error('删除失败');
        }
        else
        {
            return $this->success('删除成功');
        }

    }
    
}